package collection;

import log4j.UtilLog4j;
import org.apache.log4j.Logger;
import string.UtilString;

import java.lang.reflect.Array;
import java.util.*;

/**
 * Description:
 * Created by E440 on 2017/5/22.
 */
public class UtilCollection  {

    static Logger logger= UtilLog4j.getLogger(UtilCollection.class);

    static public void fillElements(Collection collection,Object value){

        int size=collection.size();
        fillElements(collection,size,value);
    }

    static public int removeNull(Collection collection){
        return  removeEvery(collection,null);
    }


    static public int removeEvery(Collection collection,Object o){
        int i=0;
        Iterator it=collection.iterator();
        while (it.hasNext()){
            Object o1=it.next();
            if((o1 ==null && o== null) || o1.equals(o)){
                it.remove();
                i++;
            }
        }
        return i;
    }

    /** 给指定集合填充，指定数量的，指定数值
     * @param collection
     * @param size
     * @param value
     * @return
     */
    static public void fillElements(Collection collection,int size,Object value){
        if(collection==null || size<0){
            throw  new  RuntimeException("collection is null or size <0");
        }
        collection.clear();
        for (int i  = 0; i <size ; i++) {
            collection.add(value);
        }

    }

    static  public List<String> toStringList(Collection collection){
        if(collection==null){
            return null;
        }
        List<String> list=new ArrayList<>(collection.size());
        for (Object o : collection) {
            if(o==null){
                continue;
            }
            String s=o.toString();
            list.add(s);
        }
        return list;
    }

    static  public List<String> toStringList(Object[] objects){
        if(objects==null){
            return null;
        }
        List<String> list=new ArrayList<>(objects.length);
        for (Object o : objects) {
            if (o != null) {
                String s=o.toString();
                list.add(s);
            }else {
                list.add(null);
            }
        }
        return list;
    }

    public static boolean isNullOrEmpty(Collection collection) {
        if(collection==null){
            return true;
        }
        if(collection.size()==0){
            return true;
        }
        return false;
    }

    public static <T extends Comparable<? super T>> String numberList2String(List<T> list,String orderType,String divider){
        String result="";
        if(isNullOrEmpty(list)){
            return result;
        }
        Collections.sort(list);
        if(orderType.equalsIgnoreCase("asc")){
            for(T t:list){
                result+=t.toString()+divider;
            }
            result= UtilString.deleteTail(result, divider.length());
        }

        if(orderType.equalsIgnoreCase("desc")){
            for(int i=list.size()-1;i>=0;i--){
                result+=list.get(i).toString()+divider;
            }
            result=UtilString.deleteTail(result, divider.length());
        }
        return result;
    }

    public static  String Convert2String(List<String> list,String divider){
        String result;
        if(UtilString.isNullOrEmpty(divider)){
            throw new RuntimeException("divider is null or empty.");
        }
        if(list==null || list.size()==0){
            return "";
        }else {
            Collections.sort(list);
            StringBuffer stringBuffer=new StringBuffer();
            for(Object object:list){
                if(object!=null){
                    stringBuffer.append(object.toString());
                }
                stringBuffer.append(divider);
            }
            result=stringBuffer.toString();
            result=UtilString.deleteTail(result, divider.length());
        }
        return result;
    }

    public static <T> T[] collection2Array(Class<T> clazz,Collection<T> collection) {
        T[] array=null;
        int size=0;
        if(!isNullOrEmpty(collection)){
            size=collection.size();
        }
        array=(T[]) Array.newInstance(clazz,size);
        int i=0;
        for(T t:collection){
            array[i]=t;
            i++;
        }
        return array;
    }

    public static void main(String...args){
        List<String> list=new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        String[] strings=collection2Array(String.class,list);
        for(String s:strings){
            System.out.println(s);
        }

        List<Integer> integers=new ArrayList<>();
        integers.add(null);
        integers.add(2);
        integers.add(13);
        integers.add(0);
        String resultString=numberList2String(integers,"desc",",");
        System.out.println(resultString);
    }
}
